package com.mbc.rmt.dao;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;

import com.mbc.common.dao.DAO;
import com.mbc.rmt.data.CashReturnDetailDataList;
import com.mbc.rmt.entities.CashReturnDetails;

public class CashReturnDetailDAOImpls extends DAO<CashReturnDetails> implements CashReturnDetailDAO {
	
	public CashReturnDetailDAOImpls(Connection conn) {
		this.conn = conn;
		this.table_name = "CashReturnDetails";
	}
	
	public static final String field_slipdetailid = "SlipDetailID";
	public static final String field_slipid = "SlipID";
	public static final String field_paymentid = "PaymentID";
	public static final String field_paymentdetailid = "PaymentDetailID";
	public static final String field_paymentno = "PaymentNo";
	public static final String field_paymentdate = "PaymentDate";
	public static final String field_invoiceno = "InvoiceNo";
	public static final String field_receiptno = "ReceiptNo";
	public static final String field_sender = "Sender";
	public static final String field_receiver = "Receiver";
	public static final String field_address = "Address";
	public static final String field_currencyid = "CurrencyID";
	public static final String field_currencycode = "CurrencyCode";
	public static final String field_rate = "Rate";
	public static final String field_amount = "Amount";
	public static final String field_definedetailid = "DefineDetailID";
	

	@Override
	public void create(CashReturnDetails entity) throws SQLException {
		query = insertParam(table_name, new String[] { field_slipdetailid, field_slipid, field_paymentid, field_paymentdetailid,
				field_paymentno, field_paymentdate, field_invoiceno, field_receiptno, field_sender, field_receiver, field_address, field_currencyid,
				field_amount, field_definedetailid });
		pstmt = conn.prepareStatement(query);
		updateRecord(entity);
		pstmt.executeUpdate();
		pstmt.close();
	}

	@Override
	public void update(CashReturnDetails entity) throws SQLException {
		query = updateParam(table_name, new String[] { field_slipdetailid, field_slipid, field_paymentid, field_paymentdetailid,
				field_paymentno, field_paymentdate, field_invoiceno, field_receiptno, field_sender, field_receiver, field_address, field_currencyid,
				field_amount, field_definedetailid }) + " AND " + field_slipdetailid + " = " + entity.getSlipdetailid();
		pstmt = conn.prepareStatement(query);
		updateRecord(entity);
		pstmt.executeUpdate();
		pstmt.close();
	}
	
	protected void updateRecord(CashReturnDetails entity) throws SQLException {
		int i = 1;
		pstmt.setLong(i++, entity.getSlipdetailid());
		pstmt.setLong(i++, entity.getSlipid());
		pstmt.setLong(i++, entity.getPaymentid());
		pstmt.setLong(i++, entity.getPaymentdetailid());
		pstmt.setString(i++, entity.getPaymentno());
		pstmt.setTimestamp(i++, new Timestamp(entity.getPaymentdate().getTime()));
		pstmt.setString(i++, entity.getInvoiceno());
		pstmt.setString(i++, entity.getReceiptno());
		pstmt.setString(i++, entity.getSender());
		pstmt.setString(i++, entity.getReceiver());
		pstmt.setString(i++, entity.getAddress());
		if(entity.getCurrencyid()!=null)
			pstmt.setLong(i++, entity.getCurrencyid());
		else
			pstmt.setString(i++, null);
		pstmt.setDouble(i++, entity.getAmount());
		pstmt.setLong(i++, entity.getDefinedetailid());
	}

	@Override
	public void delete(long key, int mode) throws SQLException {
		
		if(mode == HEARDER)
			query = getDeleteQuery() + " WHERE " + field_slipid + " = " + key;
		else if(mode == DETAIL)
			query = getDeleteQuery() + " WHERE " + field_slipdetailid + " = " + key;
		
		pstmt = conn.prepareStatement(query);
		pstmt.executeUpdate();
		pstmt.close();
	}

	@Override
	public CashReturnDetails findById(long slipdetailId) throws SQLException {
		CashReturnDetails l_entity = null;
		query = "SELECT crd.*, c.CurrencyCode FROM " + table_name + " crd left JOIN Currency c ON c.CurrencyID = crd.CurrencyID WHERE " + field_slipdetailid + " = " +  slipdetailId;
		pstmt = conn.prepareStatement(query);
		rs = pstmt.executeQuery();
		if(rs.next()){
			l_entity = readRecord(rs);
		}
		return l_entity;
	}
	
	@Override
	public ArrayList<CashReturnDetails> findByHeaderId(long slipid) throws SQLException {
		 ArrayList<CashReturnDetails> l_arrlist = null;
		query = "SELECT crd.*, c.CurrencyCode FROM " + table_name + " crd left JOIN Currency c ON c.CurrencyID = crd.CurrencyID WHERE " + field_slipid + " = " +  slipid;
		pstmt = conn.prepareStatement(query);
		rs = pstmt.executeQuery();
		if(rs.next()){
			l_arrlist = new ArrayList<CashReturnDetails>();
			do {
				
				l_arrlist.add(readRecord(rs));
				
			} while(rs.next());
		}
		return l_arrlist;
	}

	@Override
	public CashReturnDetailDataList findCashReturnDetailByCri(CashReturnDetails cri) throws SQLException {
		//CashReturnDetailDataList l_dataList = new CashReturnDetailDataList();
		//ArrayList<CashReturnDetails> l_list = new ArrayList<CashReturnDetails>();
		
		return null;
	}

	@Override
	protected CashReturnDetails readRecord(ResultSet rs) throws SQLException {
		CashReturnDetails l_entity = new CashReturnDetails();
		l_entity.setSlipdetailid(rs.getLong(field_slipdetailid));
		l_entity.setSlipid(rs.getLong(field_slipid));
		l_entity.setPaymentid(rs.getLong(field_paymentid));
		l_entity.setPaymentdetailid(rs.getLong(field_paymentdetailid));
		l_entity.setPaymentno(rs.getString(field_paymentno));
		l_entity.setPaymentdate(rs.getDate(field_paymentdate));
		l_entity.setInvoiceno(rs.getString(field_invoiceno));
		l_entity.setReceiptno(rs.getString(field_receiptno));
		l_entity.setSender(rs.getString(field_sender));
		l_entity.setReceiver(rs.getString(field_receiver));
		l_entity.setAddress(rs.getString(field_address));
		l_entity.setCurrencyid(rs.getLong(field_currencyid));
		l_entity.setCurrencycode(rs.getString(field_currencycode));
		l_entity.setAmount(rs.getDouble(field_amount));
		l_entity.setDefinedetailid(rs.getLong(field_definedetailid));
		
		return l_entity;
	}
	
	@Override
	public void updateCashPaymentReturnStatus(long slipdetail, long definedetail, double amount, byte status) throws SQLException {
		double l_amount = 0;
		
		query = updateParam("CashPaymentDetails", new String[] { "ReturnStatus" }) + " AND SlipDetailID = " + slipdetail;
		pstmt = conn.prepareStatement(query);
		pstmt.setByte(1, status);
		
		pstmt.executeUpdate();
		
		query = "SELECT * FROM DefinePaymentTypeDetail WHERE DetailID = " + definedetail;
		pstmt = conn.prepareStatement(query);
		rs = pstmt.executeQuery();
		if(rs.next())
			l_amount = rs.getDouble("PaidAmount");
		
		if(status==1)
			l_amount = l_amount - amount;
		else
			l_amount = l_amount + amount;
		
		query = updateParam("DefinePaymentTypeDetail", new String[] { "PaidAmount","ReturnStatus" }) + " AND DetailID = " + definedetail;
		pstmt = conn.prepareStatement(query);
		pstmt.setDouble(1, l_amount);
		if(status == 1)
			pstmt.setByte(2, status);
		else
			pstmt.setString(2, null);
		
		pstmt.executeUpdate();
		
		pstmt.close();
	}

}
